package com.bs.bszhsq.controller;

import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Users;
import com.bs.bszhsq.service.IUsersService;
import com.bs.bszhsq.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import com.bs.bszhsq.service.IDiscusssService;
import com.bs.bszhsq.entity.Discusss;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * <p>
 * 社区信息评论表 前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-04-23
 */
@RestController
@RequestMapping("/discusss")
public class DiscusssController {
    //service注入
    @Resource
    private IDiscusssService discusssService;
   @Resource
   private IUsersService usersService;

    @PostMapping
    @ApiOperation(value = "新增")
    public R save(@RequestBody Discusss discusss) {
        //评论时间
        discusss.setAddtime(LocalDateTime.now());
        //评论人
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        String userId = JWT.decode(token).getAudience().get(0);
        Users user= usersService.getById(userId);
        discusss.setUserid( user.getId());
        if (discusss.getPid()!=null){
            Discusss discusss1 = discusssService.getById(discusss.getPid());
            if (discusss1.getOrginId()!=null){
                discusss.setOrginId(discusss1.getOrginId());
            }else {
                discusss.setOrginId(discusss.getPid());
            }
        }
        boolean save = discusssService.save(discusss);
        return R.write(save);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Long id) {
        boolean b = discusssService.removeById(id);
        return R.write(b);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public R update(@RequestBody Discusss discusss) {
        boolean update = discusssService.updateById(discusss);
        return R.write(update);
    }


    @PostMapping("/deletebatch")
    @ApiOperation(value = "批量删除")
    public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = discusssService.removeByIds(ids);
        return R.write(b);
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        IPage<Discusss> IPage = discusssService.page(new Page<>(pageNum, pageSize));
        return R.success(IPage);
    }
    @GetMapping("/tree/{forumId}")
    @ApiOperation(value = "查询全部评论")
    public R findTree(@PathVariable Long forumId){
        List<Discusss> list= discusssService.getDiscusss(forumId); //全部评论信息
        //过滤出一级评论，不包括回复
        List<Discusss> discusssList = list.stream().filter(discusss -> discusss.getOrginId()==null).collect(Collectors.toList());

        for (Discusss orgin : discusssList) {
            List<Discusss> collect = list.stream().filter(discusss -> orgin.getId().equals(discusss.getOrginId())).collect(Collectors.toList());
            collect.forEach(discusss -> {
                list.stream().filter(discusss1 -> discusss1.getId().equals(discusss.getPid())).findFirst().ifPresent((v ->{
                    discusss.setPusername(v.getUsername());
                }));

            });
            //设置回复内容
            orgin.setChildren(collect);
        }
        return R.success(discusssList);
    }


}
